package edu.uwlax.cs.oayonlinestore.client.gui.admin;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.FormTable;
import edu.uwlax.cs.oayonlinestore.client.utils.HSpacer;
import edu.uwlax.cs.oayonlinestore.client.utils.Title;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.CategoryVO;
import edu.uwlax.cs.oayonlinestore.vo.UserVO;

public class PageAdminEditCategory extends TitlePage {

	CategoryVO cat;

	UserVO[] managers;

	UserVO[] people;

	TextBox t;

	TextArea ta;

	private class CategoryEdit extends FormTable {

		public CategoryEdit() {
			super();
			addInput("Name", t = new TextBox());
			addInput("Description", ta = new TextArea());
			addButton("Save", null);
		}

		public void onButtonClicked() {

			cat.setTitle(t.getText());
			cat.setDescription(ta.getText());
			OnlinestoreRService.Util.getInstance().addEditCategory(cat,
					getSaveHandler());
		}

		public void onDataLoad(Object data) {
		}
	}

	private class ManagePeopleTable extends DekoTable {
		ListBox users;

		Button addbut, removebut;

		public ManagePeopleTable() {
			addTitle("First Name");
			addTitle("Last Name");
			addTitle("");

			users = new ListBox();
			users.setSelectedIndex(0);

			AddRow r = addRow();
			r.add(users).setColspan(2).setHAlign(r.CENTER);
			r.add(addbut = new Button("Add"));
			addbut.addClickListener(new ButtonClickListener(null));
		}

		public void addPerson(UserVO u) {
			AddRow r = addRow(1);
			r.add(u.getFirstname());
			r.add(u.getLastname());
			r.add(removebut = new Button("Remove"));

			removebut.addClickListener(new ButtonClickListener(u));
		}

		public class ButtonClickListener implements ClickListener {
			UserVO vo = null;

			public ButtonClickListener(UserVO u) {
				if (u != null) {
					vo = u;
				}
			}

			public void onClick(Widget arg0) {

				if (vo == null) {
					UserVO uu = people[mans.users.getSelectedIndex()];
					OnlinestoreRService.Util.getInstance()
							.addManagerToCategory(cat.getId(), uu.getId(),
									new DoneAddAsycHandler(uu, false));

				} else {
					OnlinestoreRService.Util.getInstance()
							.removeManagerFromCategory(cat.getId(), vo.getId(),
									new DoneRemoveAsycHandler(vo, true));

				}
			}
		}
	}

	private class StaffPeopleTable extends DekoTable {
		ListBox users;

		Button addbut, removebut;

		public StaffPeopleTable() {
			addTitle("First Name");
			addTitle("Last Name");
			addTitle("");

			users = new ListBox();
			users.setSelectedIndex(0);

			AddRow r = addRow();
			r.add(users).setColspan(2).setHAlign(r.CENTER);
			r.add(addbut = new Button("Add"));
			addbut.addClickListener(new ButtonClickListener(null));
		}

		public void addPerson(UserVO u) {
			AddRow r = addRow(1);
			r.add(u.getFirstname());
			r.add(u.getLastname());
			r.add(removebut = new Button("Remove"));
			removebut.addClickListener(new ButtonClickListener(u));
		}

		public class ButtonClickListener implements ClickListener {
			UserVO vo = null;

			public ButtonClickListener(UserVO u) {
				if (u != null) {
					vo = u;
				}
			}

			public void onClick(Widget arg0) {

				if (vo == null) {
					UserVO uu = people[staffs.users.getSelectedIndex()];
					OnlinestoreRService.Util.getInstance().addStaffToCategory(
							cat.getId(), uu.getId(),
							new DoneAddAsycHandler(uu, true));

				} else {
					OnlinestoreRService.Util.getInstance()
							.removeStaffFromCategory(cat.getId(), vo.getId(),
									new DoneRemoveAsycHandler(vo, false));
				}
			}
		}
	}

	Title title;

	CategoryEdit ce;

	public class DoneAddAsycHandler extends DefaultAsyncCallback {
		UserVO uu;

		boolean flag;

		public DoneAddAsycHandler(UserVO vo, boolean flag2) {
			uu = vo;
			flag = flag2;
		}

		public void onSuccess(Object o) {
			if (flag)
				staffs.addPerson(uu);
			else
				mans.addPerson(uu);
		}
	}

	public class DoneRemoveAsycHandler extends DefaultAsyncCallback {
		UserVO uu;

		boolean flag;

		public DoneRemoveAsycHandler(UserVO vo, boolean flag2) {
			uu = vo;
			flag = flag2;
		}

		public void onSuccess(Object o) {
			clearTable();
			onShow(cat.getId());

		}
	}

	public class CategoryAsycHandler extends DefaultAsyncCallback {

		public void onSuccess(Object o) {
			cat = (CategoryVO) o;
			t.setText(cat.getTitle());
			ta.setText(cat.getDescription());
		}
	}

	public class CategoryManagersAsycHandler extends DefaultAsyncCallback {

		public void onSuccess(Object o) {

			UserVO[] managers = (UserVO[]) o;

			for (int i = 0; i < managers.length; i++)
				mans.addPerson(managers[i]);
		}
	}

	public class CategoryStaffMembersAsycHandler extends DefaultAsyncCallback {

		public void onSuccess(Object o) {
			UserVO[] staff = (UserVO[]) o;
			for (int i = 0; i < staff.length; i++)
				staffs.addPerson(staff[i]);
		}
	}

	public class CategoryStaffPeopleAsycHandler extends DefaultAsyncCallback {

		public void onSuccess(Object o) {
			people = (UserVO[]) o;
			for (int i = 0; i < people.length; i++) {
				staffs.users.addItem(people[i].getFirstname() + " "
						+ people[i].getLastname());
				mans.users.addItem(people[i].getFirstname() + " "
						+ people[i].getLastname());
			}
		}
	}

	ManagePeopleTable mans;

	StaffPeopleTable staffs;

	protected void create() {

		add(ce = new CategoryEdit());
		add(new HSpacer());
		add(new HTML("<b>Managers</b>"));
		add(mans = new ManagePeopleTable());
		add(new HSpacer());
		add(new HTML("<b>Staff Members</b>"));
		add(staffs = new StaffPeopleTable());

	}

	public String getID() {
		return "AdminEditCategory";
	}

	public boolean onShow(int param) {
		int id = param;
		setTitle((id == 0) ? "Create Category" : "Edit Category");
		if (id != 0) {

			clearTable();

			OnlinestoreRService.Util.getInstance().getCategory(id,
					new CategoryAsycHandler());
			OnlinestoreRService.Util.getInstance().getCategoryManagerList(id,
					new CategoryManagersAsycHandler());
			OnlinestoreRService.Util.getInstance().getCategoryStaffList(id,
					new CategoryStaffMembersAsycHandler());
		} else {
			t.setText("");
			ta.setText("");
			cat = new CategoryVO(0, "", "");
			mans.setVisible(false);
			staffs.setVisible(false);

		}
		OnlinestoreRService.Util.getInstance().getCategoryPeopleList(
				new CategoryStaffPeopleAsycHandler());
		return true;

	}

	protected void onDataArrived(Object o) {
	}

	public void clearTable() {
		removeAll();
		getPanel().add(new Title("Edit/Create Category"));
		setTitle("Edit/Create Category");
		add(ce = new CategoryEdit());
		add(new HSpacer());
		add(new HTML("<b>Managers</b>"));
		add(new HSpacer("4px"));
		add(mans = new ManagePeopleTable());
		add(new HSpacer());
		add(new HTML("<b>Staff Members</b>"));
		add(new HSpacer("4px"));
		add(staffs = new StaffPeopleTable());

	}

}
